//
//  ZGLPlayEditVC.m
//  Faker
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLPlayEditVC.h"
#import "ZGLPlayHomeSecondTVC.h"
#import "ZGLPlayDetailModel.h"
#import <UIImageView+AFNetworking.h>
#import "ZGLPlayDetailWebVC.h"

@interface ZGLPlayEditVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
@end

@implementation ZGLPlayEditVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.tableView.backgroundColor = backColor;
    self.view.backgroundColor = backColor;
    [self.tableView reloadData];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
/* 内存管理 */
-(void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}
#pragma mark 1 - 主程序入口
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zaihufanhui"] style:UIBarButtonItemStyleDone target:self action:@selector(leftButton)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    [self creatTableView];
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 60 * ScaleX, 40 * ScaleX)];
    self.activityIndicatorView.center = self.view.center;
    [self.view addSubview:self.activityIndicatorView];
    self.activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [self.activityIndicatorView startAnimating];
    // Do any additional setup after loading the view.
}
/* 返回键点击事件 */
-(void)leftButton {
    [self.navigationController popViewControllerAnimated:YES];
    
}
/* 属性set方法重写 */
-(void)setModelArr:(NSMutableArray *)modelArr {
    if (_modelArr != modelArr ) {
        _modelArr = modelArr;
    }
    [self.activityIndicatorView stopAnimating];
    [self.tableView reloadData];
}
#pragma mark 2 - tableView部分
/* 创建对象 */
-(void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, Height) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[ZGLPlayHomeSecondTVC class] forCellReuseIdentifier:@"ZGLPlayEditVC_Cell"];
    
}
/* 行数 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
/* 分区数 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.modelArr.count;
}
/* cell赋值 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZGLPlayDetailModel *model = [self.modelArr objectAtIndex:indexPath.section];
    ZGLPlayHomeSecondTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLPlayEditVC_Cell"];
    cell.titleLabel.text = model.title;
    cell.contentLabel.text = model.s_title;
    [cell.mainImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", model.thumbnail_medias[0][@"url"]]] placeholderImage:[UIImage imageNamed:@"place1.jpg"]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 260;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}
/* cell选中点击事件 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZGLPlayDetailModel *model = [self.modelArr objectAtIndexedSubscript:indexPath.section];
    ZGLPlayDetailWebVC *webVC = [[ZGLPlayDetailWebVC alloc] init];
    webVC.webStr = model.weekend[@"content_url"];
    [self.navigationController pushViewController:webVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
